package com.windfone.web.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.model.Member;
import com.windfone.model.Orderhistory;
import com.windfone.service.MemberService;

public class PurchaseHistoryAction extends ActionSupport {
	private MemberService memberService;
	private int memberId;
	private int producttype;
	private int productId;
	private String price;
	private String productName;
	private String buydate;
	private String remark;	
	private String serialNumber;
	private String callback;
	public String getCallback() {
		return callback;
	}

	public void setCallback(String callback) {
		this.callback = callback;
	}
	private List<Orderhistory> orderhistorys;
	private Member member;
	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public List<Orderhistory> getOrderhistorys() {
		return orderhistorys;
	}

	public void setOrderhistorys(List<Orderhistory> orderhistorys) {
		this.orderhistorys = orderhistorys;
	}

	public int getProducttype() {
		return producttype;
	}

	public int getProductId() {
		return productId;
	}

	public String getPrice() {
		return price;
	}

	public String getProductName() {
		return productName;
	}

	public String getBuydate() {
		return buydate;
	}

	public String getRemark() {
		return remark;
	}

	public void setProducttype(int producttype) {
		this.producttype = producttype;
	}

	public String getSerialNumber() {
		return serialNumber;
	}

	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
	}

	public void setProductId(int productId) {
		this.productId = productId;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public void setBuydate(String buydate) {
		this.buydate = buydate;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public int getMemberId() {
		return memberId;
	}

	public void setMemberId(int memberId) {
		this.memberId = memberId;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	
	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;

	public String execute() throws Exception {
		return super.execute();
	}
	public String loadPurchaseHistory() throws Exception {
		orderhistorys = memberService.listOrderHistoryByUserId( memberId );
		member = memberService.getMemberByKey(memberId);
		return  SUCCESS;
	}
	
	private String operateType ;
	private int purchaseHistoryId;
	public int getPurchaseHistoryId() {
		return purchaseHistoryId;
	}

	public void setPurchaseHistoryId(int purchaseHistoryId) {
		this.purchaseHistoryId = purchaseHistoryId;
	}

	public String getOperateType() {
		return operateType;
	}

	public void setOperateType(String operateType) {
		this.operateType = operateType;
	}

	public String addPurchaseHistory() throws Exception {
		Orderhistory bean = new  Orderhistory();
		if ( callback!=null)
			bean.setCallback(callback);
		else 
			bean.setCallback("");
		bean.setBuydate(buydate);
		bean.setUserId(memberId);
		bean.setPrice(price);
		bean.setProductName(productName);
		bean.setProducttype(producttype);
		bean.setRemark(remark);
		bean.setSerialNumber(serialNumber);
		member  = memberService.getMemberByKey(memberId);
		int p1 = member.getPoints();
		int p2 = member.getPointsb();
		int p3 = 0;
		if ( price !=null && !price.equals("")){
			p3 = (int)Double.parseDouble(price);
		}
		if ( operateType .equals("insert")){
			memberService.addOrderHistory( bean );
			int p4 = p1+ p3;
			int p5 = p2 + p3;
			member.setPoints( p4  );
			member.setPointsb( p5 );
			memberService.updateMember(member);
		} else {
			bean.setId(  purchaseHistoryId  );
			Orderhistory tmpBean = memberService.getOrderHistoryById( purchaseHistoryId );
			String priceTmp = tmpBean.getPrice();
			int p4=0;
			if ( priceTmp !=null && ! priceTmp.equals("")){
				p4  = (int)Double.parseDouble(priceTmp);
			}
			int p6 = p3 -p4;
			int p7 = p2 - p4;
			member.setPoints( p6  );
			member.setPointsb( p7 );
			memberService.updateMember(member);
			memberService.updateOrderHistory( bean );
		}
		orderhistorys = memberService.listOrderHistoryByUserId( memberId );
		return  SUCCESS;
	}
	
	public String deletePurchaseHistory() throws Exception {
		member  = memberService.getMemberByKey(memberId);
		Orderhistory tmpBean = memberService.getOrderHistoryById( purchaseHistoryId );
		String priceTmp = tmpBean.getPrice();
		int p4=0;
		if ( priceTmp !=null && ! priceTmp.equals("")){
			p4  = (int)Double.parseDouble(priceTmp);
		}
		int p1 = member.getPoints();
		int p2 = member.getPointsb();
		int p3 = p1 - p4;
		int p5 = p2 - p4;
		member.setPoints( p3 );
		member.setPointsb( p5 );
		memberService.updateMember(member);
		Orderhistory bean = new  Orderhistory();
		bean.setId(  purchaseHistoryId  );
		memberService.deleOrderHistory(bean);
//		orderhistorys = memberService.listOrderHistoryByUserId( memberId );
		 
		return  SUCCESS;
	}
}
